vim.api.nvim_create_autocmd("FileType", { pattern = "json", command = "set filetype=jsonc" })
vim.api.nvim_create_autocmd("FileType", { pattern = "zsh", command = "set filetype=sh" })
-- if macos
vim.api.nvim_create_autocmd(
  "FocusGained",
  { callback = function() vim.fn.setreg("+", vim.trim(vim.fn.getreg "+")) end }
)
-- vim.api.nvim_create_autocmd({ "FocusLost" }, {
--   callback = function()
--     vim.fn.execute "silent! write"
--     vim.notify("Autosaved!", vim.log.levels.INFO, {})
--   end,
-- })
vim.api.nvim_create_autocmd({ "FocusLost" }, {
  pattern = "*",
  callback = function(event)
    if event.buftype or event.file == "" then return end
    if vim.api.nvim_buf_get_option(event.buf, "modified") then
      vim.api.nvim_buf_call(event.buf, function()
        vim.schedule(function()
          vim.cmd "silent! write"
          -- vim.notify("Autosaved!", vim.log.levels.INFO, {})
        end)
      end)
    end
  end,
})

-- local function auto_install_fish_lsp()
--   local mason_ok, mason = pcall(require, "mason")
--   if not mason_ok then return end
--   local registry = require "mason-registry"
--   local pkg_name = "fish-lsp"
--   if not registry.get_package(pkg_name):is_installed() then registry.get_package(pkg_name):install {} end
-- end
--
-- vim.api.nvim_create_autocmd("VimEnter", {
--   callback = auto_install_fish_lsp,
--   pattern = "*",
-- })

-- vim.cmd [[ autocmd FocusLost * if &modified | write | endif ]]
-- local function script_path()
--   local str = debug.getinfo(2, "S").source:sub(2)
--   return str:match "(.*/)"
-- end
--
-- local cmd_path = script_path() .. "/bin/im-select" .. "-" .. vim.loop.os_uname().machine
-- local win_path = script_path() .. "\\bin\\im-select.exe"
--
-- if vim.loop.os_uname().sysname == "Darwin" then
--   local function swift_to_en() vim.loop.spawn(cmd_path, { args = { "com.apple.keylayout.ABC" } }) end
--   -- vim.api.nvim_create_autocmd("ModeChanged", { pattern = { "*:n" }, callback = function() swift_to_en() end })
--   vim.api.nvim_create_autocmd(
--     { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
--     { callback = function() swift_to_en() end }
--   )
-- end
--
-- if vim.loop.os_uname().sysname == "Linux" then
--   if vim.fn.executable "ibus" == 1 then
--     local function linux_swift_to_en() vim.loop.spawn("ibus", { args = { "engine", "xkb:us::eng" } }) end
--     vim.api.nvim_create_autocmd("ModeChanged", { pattern = { "*:n" }, callback = function() linux_swift_to_en() end })
--     vim.api.nvim_create_autocmd(
--       { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
--       { callback = function() linux_swift_to_en() end }
--     )
--   end
--   if vim.fn.executable "fcitx5-remote" == 1 then
--     local function linux_swift_to_en() vim.loop.spawn("fcitx5-remote", { args = { "-c" } }) end
--     vim.api.nvim_create_autocmd("ModeChanged", { pattern = { "*:n" }, callback = function() linux_swift_to_en() end })
--     vim.api.nvim_create_autocmd(
--       { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
--       { callback = function() linux_swift_to_en() end }
--     )
--   end
--   if vim.fn.executable "fcitx-remote" == 1 then
--     local function linux_swift_to_en() vim.loop.spawn("fcitx-remote", { args = { "-c" } }) end
--     vim.api.nvim_create_autocmd("ModeChanged", { pattern = { "*:n" }, callback = function() linux_swift_to_en() end })
--     vim.api.nvim_create_autocmd(
--       { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
--       { callback = function() linux_swift_to_en() end }
--     )
--   end
-- end
--
-- -- git-bash
-- if vim.loop.os_uname().sysname == "Windows_NT" then
--   local function win_swift_to_en() vim.loop.spawn(win_path, { args = { "1033" } }) end
--   vim.api.nvim_create_autocmd("ModeChanged", { pattern = { "*:n" }, callback = function() win_swift_to_en() end })
--   vim.api.nvim_create_autocmd(
--     { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
--     { callback = function() win_swift_to_en() end }
--   )
-- end
